﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace CMSMVC4.Areas.Main.Models
{
    public class SitemapActionResults : ActionResult
    {
        //
        // GET: /SitemapActionResult/
        private List<Post> _posts;
        public void SitemapActionResult(List<Post> posts)
        {
            this._posts = posts;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "application/rss+xml";

            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");

                foreach (Post post in this._posts)
                {
                    writer.WriteStartElement("url");
                    writer.WriteElementString("loc", string.Format("http://3dscan.com.vn{0}", post.Slug));

                    writer.WriteElementString("lastmod", post.UpdatedOn.ToString("yyyy-MM-dd"));

                    writer.WriteElementString("changefreq", "daily");
                    writer.WriteElementString("priority", "0.5");
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.Flush();
                writer.Close();
            }
        }
    }
}